<?php

class vaovao extends module {

	public $title = "Vaovao module with ini";
	public $author = "hery";
	public $version = "0.9";

	//Methods here:

	public function init()
	{
		$this->vaovao_ini = new ini("data/vaovao.ini");

	}

	
	public function reply($line, $args)
	{

		if ($args['nargs'] == 0)
		{
			$chan = "#serasera";
		}
		else
		{
			if (substr($args['arg1'], 0, 1) != "#")
			{
				$chan = "#serasera";
				$cmd = $args['arg1'];
				if (isset($args['arg2']))
				{
					$valiny = trim(substr($args['query'], (strpos($args['query'], $args['arg1'])) + (strlen($args['arg1']))));
				}
			}
			else
			{
				$chan = $args['arg1'];
				$cmd = isset($args['arg2'])?$args['arg2']: '';
				if (isset($args['arg3']))
				{
					$valiny = trim(substr($args['query'], (strpos($args['query'], $args['arg2'])) + (strlen($args['arg2']))));
				}
			}
		}
	
		switch ($cmd)
		{
			case "add";
				$this->vaovao_add($line, $chan, $cmd, $valiny);
			break;
			case "del";
				$this->vaovao_del($line, $chan, $cmd, $valiny);
			break;
			case "list";
				$this->vaovao_list($line, $chan, $cmd, $valiny);
			break;
			default:
				$this->vaovao_default($line, $chan, $cmd, $valiny);
			;
		}

	}
	
	public function vaovao_default($line, $chan, $cmd, $valiny)
	{
		if($msg = $this->vaovao_ini->getVars($chan))
		{
			$this->ircClass->privMsg($line['fromNick'], "Ireto ny vaovao ao amin'ny " . $chan . ": ");
			foreach ($msg as $key => $val)
			{
				if ($key && $val)
				{
				$this->ircClass->privMsg($line['fromNick'], BOLD  . date("d/m/Y H:i:s" , $key) . BOLD . " : " . $val);
				}
			}
		}
		else
		{
			$this->ircClass->notice($line['fromNick'], "Tsy misy vaovao hita ho an'ny $chan");
		}
	
	}
	
	public function vaovao_list($line, $chan, $cmd, $valiny)
	{

		if ($line['fromNick'] != "hery" && !$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
	
		if($msg = $this->vaovao_ini->getVars($chan))
		{
			$this->ircClass->privMsg($line['fromNick'], "Ireto ny vaovao ao amin'ny " . $chan . ": ");
			foreach ($msg as $key => $val)
			{
				if ($key && $val)
				{
				$this->ircClass->privMsg($line['fromNick'], BOLD . $key . BOLD . ": " . $val);
				}
			}
		}
		else
		{
			$this->ircClass->notice($line['fromNick'], "Tsy misy vaovao");
		}
	
	}
	
	public function vaovao_add($line, $chan, $cmd, $valiny)
	{
		if ($line['fromNick'] != "hery" && !$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
	
		if (!isset($valiny))
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nety ny fampidirana. Soraty hoe " . BOLD . "!vaovao $chan add " . BOLD . "<vaovao ampidirina>");
			return;
		}
		$valiny = BLUE . "(" . $line['fromNick'] . ") " . NOFORMAT . $valiny;
		$vars = $this->vaovao_ini->getVars($chan);
		$this->vaovao_ini->setIniVal($chan, mktime(), $valiny);
		$this->vaovao_ini->writeIni();
		$this->ircClass->notice($line['fromNick'], "Tafiditra ny vaovao");
		
		$nbvaovao = $this->vaovao_ini->numVars($chan);
		$ini = new ini("data/vonjy.ini");
		$ini->setIniVal($chan, "!vaovao", "Mampiseho ny vaovao eto amin'ny $chan (misy vaovao $nbvaovao)");
		$ini->writeIni();
	
	}

	public function vaovao_del($line, $chan, $cmd, $valiny)
	{
		if ($line['fromNick'] != "hery" && !$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
	
		if (!isset($valiny))
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nety ny famafana. Soraty hoe " . BOLD . "!vaovao $chan del " . BOLD . "<laharan'ny vaovao>. Raha tsy fantatrao ny laharana dia soraty hoe " . BOLD ."!vaovao $chan list");
			return;
		}
		$id = intval($valiny);
		if ($id == 0)
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nampiditra laharan'ny vaovao hofafaina ianao. Soraty hoe " . BOLD . "!vaovao $chan del <id> " . BOLD . "ka ny <id> dia soloina ny laharan'ny vaovao. Raha tsy fantatrao ny laharana dia soraty hoe " . BOLD . "!vaovao $chan list");
			return;
		}
		if($this->vaovao_ini->deleteVar($chan, $id))
		{
			$this->vaovao_ini->writeIni();
			$this->ircClass->notice($line['fromNick'], "Voafafa ny vaovao");
		}
		else
		{
			$this->ircClass->notice($line['fromNick'], "Tsy hita ny vaovao hofafaina");
		}

	}						
}

?>
